<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/24 0024
 * 线上线下商品
 * Time: 10:45
 */

namespace app\admin\controller\mall;

use app\common\controller\AdminController;
use think\facade\View;

class ItemOrder extends AdminController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = app('app\common\model\mall\ItemOrder');
        $this->refund = app('app\common\model\mall\ItemOrderRefund');
    }

    /**
     * 订单列表
     */
    public function get_where($arr = [])
    {
        $array[] = ['status' , '<>' , 7];
        //$array[] = ['type','in',['2','3']];
        if (!empty( $arr['order_sn'] )) {
            $array[] = ['order_sn' , 'like' , '%' . $arr['order_sn'] . '%'];
        }
        if (!empty( $arr['type'] )) {
            $array[] = ['type' , '=' , $arr['type']];
        }
        return $array;
    }

    /**
     * 详情
     */
    public function get_edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->with(['Address' , 'User' , 'Lists' => function ($query) {
            $query->with(['Goods' , 'GoodsSku']);
        }])->find($id);
        //快递公司列表
        $mes = ['ems' => 'EMS快递' ,
            'shentong' => '申通快递' ,
            'shunfeng' => '顺丰快递' ,
            'yuantong' => '圆通快递' ,
            'yunda' => '韵达快递' ,
            'huitong' => '百世汇通快递' ,
            'tiantian' => '天天快递' ,
            'zhongtong' => '中通快递' ,
            'zhaijisong' => '宅急送快递' ,
            'pingyou' => '中国邮政' ,
            'guotong' => '国通快递' ,
            'jingdong' => '京东快递' ,
            'sure' => '速尔快递' ,
            'yd' => '韵达快运'];
        View::assign('row' , $row);
        View::assign('mes' , $mes);
        return View::fetch('edit');
    }

    /**
     * 关联模型
     */
    public function get_with()
    {
        return ['User'];
    }

    /**
     * 操作订单
     */
    public function edit_order()
    {
        $params = $this->request->param();
        try {
            $info = $this->model->find($params['id']);
            $params['status'] = 2;
            $info->save($params);
            return json(['code' => 1 , 'msg' => '操作成功']);
        } catch ( \Exception $e ) {

            return json(['code' => 0 , 'msg' => '操作失败' . $e->getMessage()]);

        }
    }

    /**
     * 售后列表
     * @return \think\response\Json
     */
    public function refund_index()
    {

        return view();
    }

    /**
     * 异步获取数据
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed|\think\response\View
     */
    public function refundData()
    {

        $this->request->filter(['strip_tags']);

        $res = $this->refund->with(['Goods' , 'OrderList' , 'User'])->field($this->field ?? '*')
            ->order($this->order)->paginate($this->request->param('limit' , 100));

        // $res = $model->order('id','desc')->paginate($request->param('limit',30));

        $data = [
            'code' => 0 ,
            'msg' => '正在请求中...' ,
            'count' => $res->total() ,
            'data' => $res->items() ,
        ];
        return json($data);
    }

    /**
     * 售后详情
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed|\think\response\View
     */
    public function refund_detail()
    {

        $id = $this->request->param('id');

        $res = $this->refund->with(['Order' , 'Goods' , 'OrderList' , 'User'])->find($id);

        View::assign('row' , $res);
        return view();
    }

    /**
     * 操作售后订单
     */
    public function edit_refund()
    {
        $params = $this->request->param();
        try {
            $info = $this->refund->find($params['id']);
            switch ($params['status']) { //1：同意退款；2：驳回；3：同意退货
                case 1:
                    //处理退款 库存回滚  退款
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }

            $info->save($params);
            return json(['code' => 1 , 'msg' => '操作成功']);
        } catch ( \Exception $e ) {

            return json(['code' => 0 , 'msg' => '操作失败' . $e->getMessage()]);

        }
    }
}